<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    let p = {
      name: '开发部',
      person: [
        {
          name:'zs',
          hobby:['zs的爱好']
        },
        {
          name:'lisi',
          hobby:['lisi的爱好']
        }
      ]
    }

    // 1. 浅拷贝
    // var p2 = {};
    // for (let key in p) {
    //   p2[key] = p[key];
    // }
    // p2.person[1].hobby = "lisi的爱好更改了";
    // console.log(p2);
    // console.log(p);
    
    let pArr=[{name:'zs',hobby:['zs的爱好']},{name:'lisi',hobby:['lisi的爱好']}]
    // 2. 深拷贝
    function handleObj(obj) {  
      var target = Array.isArray(obj) ? [] : {}
      for (const key in obj) {
        // 判断属性值是否为复杂数据类型
        typeof obj[key]==='object'?target[key]=handleObj(obj[key]):target[key]=obj[key]
      }
      return target
    }

    let p3=handleObj(p)

    console.log('p',p);
    console.log('p3',p3);
    
    p3.person[1].hobby='lisi的爱好更改了'

    let p4=handleObj(pArr)
    console.log('pArr',pArr);
    console.log('p4',p4);
    p4[1].hobby[0]='lisi的爱好更改了'


    

  </script>
</body>

</html>